package com.huxin.servelt;

import com.alibaba.fastjson.JSON;
import com.huxin.pojo.Bookreservation;
import com.huxin.pojo.ConditionalQueryDTO;
import com.huxin.pojo.PageBean;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/findAllBookreservation")
public class UserFindAllBookreservationServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String s = req.getReader().readLine();
        ConditionalQueryDTO conditionalQueryDTO = JSON.parseObject(s, ConditionalQueryDTO.class);
        //查询所有不要id
        Userservice userservice = new Userservice();
        PageBean<Bookreservation> bean=null;

        int page=1;//开始页
        int size=10;//每页显示十条数据s
        if(null==conditionalQueryDTO.getPage()){
            conditionalQueryDTO.setPage(page);
        }
        if(null==conditionalQueryDTO.getSize()){
            conditionalQueryDTO.setSize(size);
        }
//        book_name
//        name
//        card
        if (conditionalQueryDTO.getText()==null||conditionalQueryDTO.getText().equals("")||conditionalQueryDTO.getText().equals("null")){
            bean=userservice.findAllreservation(page,size);
        } else if ("bid".equals(conditionalQueryDTO.getType())) {//按照书籍号查询
            bean=userservice.findAllbyBookId(conditionalQueryDTO);
        } else if ("book_name".equals(conditionalQueryDTO.getType())) {//按照书籍名查询
            bean=userservice.findAllbyBook(conditionalQueryDTO);
        } else if("username".equals(conditionalQueryDTO.getType())) {
            bean=userservice.findbyUserName(conditionalQueryDTO);
        }
        String jsonString = JSON.toJSONString(bean);
        resp.getWriter().write(jsonString);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
